package com.imooc.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.bo.DataDictionaryBO;
import com.imooc.exception.GraceException;
import com.imooc.mapper.DataDictionaryMapper;
import com.imooc.pojo.DataDictionary;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.DataDictionaryService;
import com.imooc.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2023-03-28
 */
@Service
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary> implements DataDictionaryService {

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public void createDataDictionary(DataDictionaryBO dataDictionaryBO) {
        //判断有没有重复
        LambdaQueryWrapper<DataDictionary> wrapper = new LambdaQueryWrapper<DataDictionary>()
                .eq(DataDictionary::getItemKey, dataDictionaryBO.getItemKey())
                .eq(DataDictionary::getItemValue, dataDictionaryBO.getItemValue());

        DataDictionary temp = baseMapper.selectOne(wrapper);
        if(!Objects.isNull(temp)){
            //抛异常
            GraceException.display(ResponseStatusEnum.DATA_DICT_EXIST_ERROR);
        }
        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictionaryBO, dataDictionary);
        baseMapper.insert(dataDictionary);
    }

    @Override
    public void updateDataDictionary(DataDictionaryBO dataDictionaryBO) {
        DataDictionary dataDictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictionaryBO, dataDictionary);
        baseMapper.updateById(dataDictionary);
    }

    @Override
    public PagedGridResult getDataByLimit(Integer page, Integer limit, String typeName, String itemValue) {

        LambdaQueryWrapper<DataDictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(DataDictionary::getTypeName).orderByAsc(DataDictionary::getSort);

        if(StringUtils.isNotBlank(typeName)){
            wrapper.like(DataDictionary::getTypeName, typeName).orderByAsc(DataDictionary::getSort);
        }
        if(StringUtils.isNotBlank(itemValue)){
            wrapper.like(DataDictionary::getItemValue, itemValue).orderByAsc(DataDictionary::getSort);
        }
        Page<DataDictionary> itemPage = new Page<>(page, limit);
        Page<DataDictionary> dataPage = dataDictionaryMapper.selectPage(itemPage, wrapper);
        PagedGridResult result = new PagedGridResult();
        result.setPage(page);
        result.setLimit(limit);
        result.setRows(dataPage.getRecords()); //每页显示内容
        result.setRecords(dataPage.getTotal()); //总记录数
        result.setTotal(dataPage.getCurrent()); //总页数
        return result;
    }

    @Override
    public DataDictionary getItem(String dictId) {
//        int i = baseMapper.deleteById(dictId);
        return baseMapper.selectById(dictId);
    }

    @Override
    public void delete(String dictId) {
        int i = baseMapper.deleteById(dictId);
        if(i == 0){
            GraceException.display(ResponseStatusEnum.DATA_DICT_DELETE_ERROR);
        }
    }

    @Override
    public List<DataDictionary> getDataByCode(String typeCode) {
        LambdaQueryWrapper<DataDictionary> wrapper = new LambdaQueryWrapper<DataDictionary>()
                .eq(DataDictionary::getTypeCode, typeCode)
                .orderByAsc(DataDictionary::getSort);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<DataDictionary> getData(String[] keys) {
        LambdaQueryWrapper<DataDictionary> wrapper = new LambdaQueryWrapper<DataDictionary>()
                .eq(DataDictionary::getEnable, 1)
                .in(DataDictionary::getItemKey, keys);
        return dataDictionaryMapper.selectList(wrapper);
    }
}
